/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * OfficeAction.java
 * wholebasic
 * 2013-7-19上午11:42:39
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.struts2.workflow;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.OfficeDataRegion;
import org.jackysoft.entity.User;
import org.jackysoft.service.OfficeDataRegionService;
import org.jackysoft.util.SpringSecurityUtils;
import org.jackysoft.workflow.WorkItem;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

import com.google.common.collect.Lists;
import com.zhuozhengsoft.pageoffice.BorderStyleType;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordreader.DataRegion;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;


@Namespace("/workflow/document")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@InterceptorRefs({ @InterceptorRef("defaultStack"),@InterceptorRef("annotationWorkflow") })
public class DocumentAction  extends AbstractWorkflowAction<WorkItem>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3297502489397848192L;
    
	protected String openurl;	
	protected int    dispatcher;
	private OfficeDataRegionService dataService;
	

	protected OpenModeType openModeType = OpenModeType.docNormalEdit;
	
	public String viewoffice() throws Exception{
		String basePath = httpRequest.getScheme() + "://" + httpRequest.getServerName()
				+ ":" + httpRequest.getServerPort() + httpRequest.getContextPath() + "/";
		WordDocument doc = new WordDocument();
				
		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(httpRequest);
		
		Collection<OfficeDataRegion>  tags = dataService.getDataRegionsByWorkflow(workflowId);
		for(OfficeDataRegion tag:tags){
		   
		   com.zhuozhengsoft.pageoffice.wordwriter.DataRegion dr = doc.openDataRegion(tag.getName());
		   dr.setEditing(true);
		   log.info(String.format("workflow:%d dataregion %s editable is %s" , workflowId,dr.getName(),dr.getEditing()));
		   
		
		}	
		poCtrl1.setServerPage(basePath+"poserver.do"); //此行必须
		// 设置界面样式
		poCtrl1.setCaption("庆华公司文件审批模版");
		poCtrl1.setBorderStyle(BorderStyleType.BorderThin);
		// 添加自定义工具条按钮
		poCtrl1.addCustomToolButton("返回", "poPrev()", 3);		
		poCtrl1.addCustomToolButton("保存并提交", "poSave()", 1);		
		poCtrl1.addCustomToolButton("直接提交", "poNext()", 6);
        
		poCtrl1.setJsFunction_AfterDocumentSaved("afterSave()");
	
		//隐藏工具栏
		poCtrl1.setOfficeToolbars(true);
		poCtrl1.setCustomRibbon("ribbon");
		poCtrl1.setMenubar(true);

		// 设置保存文档的服务器页面
		processName = workflow.getWorkflowName(workflowId);
		poCtrl1.setSaveDataPage(basePath+"workflow/document/document!savemeta?workflowId="+workflowId);
		poCtrl1.setSaveFilePage(basePath+"admin/office/office-manager!savedoc?workflowId="+workflowId);

		poCtrl1.setWriter(doc);
		// 打开文档
		//poCtrl1.webOpen("law-contract!openstream", OpenModeType.docSubmitForm,"Tom");
		User owner = SpringSecurityUtils.getCurrentUser();
		poCtrl1.webOpen(basePath+openurl, openModeType,owner.getLocalName());
			
		poCtrl1.setTagId("PageOfficeCtrl1"); //此行必须	    
		
		
		return "viewoffice";
	}
	
	public String savemeta() throws Exception{
		com.zhuozhengsoft.pageoffice.wordreader.WordDocument doc
		  = new com.zhuozhengsoft.pageoffice.wordreader.WordDocument(httpRequest,httpResponse);
		ArrayList drs = doc.getDataRegions();
		Collection<OfficeDataRegion> tags = Lists.newArrayList();
		String processName = workflow.getWorkflowName(workflowId);				
		for(Object o :drs){
			DataRegion dr = (DataRegion) o;
			log.info("data region : "+dr.getName()+"=>"+dr.getValue());
			tags.add(new OfficeDataRegion("PO_"+dr.getName(),dr.getValue(),processName,workflowId));
		}
		dataService.removeOfficeDataRegionsByWorkflow(workflowId);
		dataService.create(tags);			
		httpRequest.setAttribute("doc", doc);		
		return "savedocmeta";
	}
	

	public String getOpenurl() {
		return openurl;
	}

	
	public int getDispatcher() {
		return dispatcher;
	}

	public OpenModeType getOpenModeType() {
		return openModeType;
	}

	public void setOpenurl(String openurl) {
		this.openurl = openurl;
	}
	
	public void setDispatcher(int dispatcher) {
		this.dispatcher = dispatcher;
	}

	public void setOpenModeType(OpenModeType openModeType) {
		this.openModeType = openModeType;
	}

	@Resource
	public void setDataService(OfficeDataRegionService dataService) {
		this.dataService = dataService;
	}	

}
